package com.demo.mqtt.common.utils;

import javolution.io.Struct;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * iot协议转换相关工具
 * </p>
 *
 * @author gobigg
 * @since 2020/9/10
 */
@Slf4j
public class IotStringUtil extends StringUtil {

    public static LocalDateTime getTimeFromUnsigned8(Struct.Unsigned8[] times) {
        if (times == null || times.length != 7) {
            return null;
        }
        int year = Integer.parseInt(String.valueOf(times[0].get()) + String.valueOf(times[1].get()));
        return LocalDateTime.of(year,
                times[2].get(),
                times[3].get(),
                times[4].get(),
                times[5].get(),
                times[6].get());
    }

    public static LocalDateTime getTimeFromUnsigned8New(String times) {
        if (times == null || times.length() != 10) {
            return null;
        }
        long l = Long.parseLong(times, 16);
        String time = "20" + String.valueOf(l);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        LocalDateTime ldt = LocalDateTime.parse(time, dtf);
        return ldt;
    }



}
